unit DbCreate;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FormMDIChild, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinFoggy, dxSkinGlassOceans,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSharp, dxSkinSilver, dxSkinSpringTime,
  dxSkinStardust, dxSkinSummer2008, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinXmas2008Blue, dxSkinscxPCPainter, dxLayoutControl, cxContainer, cxEdit,
  dxLayoutcxEditAdapters, cxTextEdit, Menus, StdCtrls, cxButtons, cxMemo,
  IniFiles, dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxLayoutControlAdapters,
  dxLayoutContainer;

type
  TfmDbCreate = class(TfmMDIChild)
    edTablespace: TcxTextEdit;
    liTablespace: TdxLayoutItem;
    edDatabaseDir: TcxTextEdit;
    liDatabaseDir: TdxLayoutItem;
    edDatabaseSize: TcxTextEdit;
    liDatabaseSize: TdxLayoutItem;
    lcWindowGroup1: TdxLayoutGroup;
    edExtendSize: TcxTextEdit;
    lcWindowItem3: TdxLayoutItem;
    lcWindowGroup2: TdxLayoutGroup;
    edDatabaseMaxSize: TcxTextEdit;
    lcWindowItem1: TdxLayoutItem;
    edUserAdm: TcxTextEdit;
    liUserAdm: TdxLayoutItem;
    edUserAdmPassword: TcxTextEdit;
    liUserAdmPassword: TdxLayoutItem;
    lcWindowGroup4: TdxLayoutGroup;
    edUserRole: TcxTextEdit;
    liUserRole: TdxLayoutItem;
    mmoSQL: TcxMemo;
    lcWindowItem2: TdxLayoutItem;
    cxButton1: TcxButton;
    lcWindowItem4: TdxLayoutItem;
    procedure cxButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

{
var
  fmDatabaseCreate: TfmDatabaseCreate;
}

const
  scCreateOptions = 'CREATE OPTIONS';

implementation
uses AppConst;

{$R *.dfm}

procedure TfmDbCreate.cxButton1Click(Sender: TObject);
var
  s: string;
  p: integer;
  IniFile: TIniFile;
begin
  inherited;
  mmoSQL.Lines.LoadFromFile(AppRootDir + 'Template\Database\Create.sql');

  p := Length(edDatabaseDir.Text);
  if edDatabaseDir.Text[p] = '\' then begin
    s := edDatabaseDir.Text;
    Delete(s, p, 1);
    edDatabaseDir.Text := s;
  end;

  s := mmoSQL.Lines.Text;

  s := StringReplace(s, '$TABLESPACE', edTablespace.Text, [rfReplaceAll, rfIgnoreCase]);
  s := StringReplace(s, '$DB_DIR', edDatabaseDir.Text, [rfReplaceAll, rfIgnoreCase]);
  s := StringReplace(s, '$DB_SIZE', edDatabaseSize.Text, [rfReplaceAll, rfIgnoreCase]);
  s := StringReplace(s, '$DB_EXTEND_SIZE', edExtendSize.Text, [rfReplaceAll, rfIgnoreCase]);
  s := StringReplace(s, '$DB_MAX_SIZE', edDatabaseMaxSize.Text, [rfReplaceAll, rfIgnoreCase]);

  s := StringReplace(s, '$USER_ADM', edUserAdm.Text, [rfReplaceAll, rfIgnoreCase]);
  s := StringReplace(s, '$USER_PASSWORD', edUserAdmPassword.Text, [rfReplaceAll, rfIgnoreCase]);
  s := StringReplace(s, '$USER_ROLE', edUserRole.Text, [rfReplaceAll, rfIgnoreCase]);

  mmoSQL.Lines.Text := s;

  IniFile := TiniFile.Create(AppRootDir + edUserAdm.Text + '@' + edTablespace.Text + '.ini');
  try
    IniFile.WriteString(scCreateOptions, 'TABLESPACE', edTablespace.Text);
    IniFile.WriteString(scCreateOptions, 'DB_DIR', edDatabaseDir.Text);
    IniFile.WriteString(scCreateOptions, 'DB_SIZE', edDatabaseSize.Text);
    IniFile.WriteString(scCreateOptions, 'DB_EXTEND_SIZE', edExtendSize.Text);
    IniFile.WriteString(scCreateOptions, 'DB_MAX_SIZE', edDatabaseMaxSize.Text);

    IniFile.WriteString(scCreateOptions, 'USER_ADM', edUserAdm.Text);
    IniFile.WriteString(scCreateOptions, 'USER_PASSWORD', edUserAdmPassword.Text);
    IniFile.WriteString(scCreateOptions, 'USER_ROLE', edUserRole.Text);
  finally
    IniFile.Free;
  end;

end;

end.
